//
//  ZQQSegmentBarVC.m
//  ZQQSegmentBar
//
//  Created by MoDao-iOS on 2017/7/14.
//  Copyright © 2017年 wushengdubai. All rights reserved.
//

#import "ZQQSegmentBarVC.h"
#import "UIView+ZQQSegmentBar.h"

@interface ZQQSegmentBarVC ()<ZQQSegmentBarDelegate, UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *contentView;
@end

@implementation ZQQSegmentBarVC

- (ZQQSegmentBar *)segmentBar {
    if (!_segmentBar) {
        _segmentBar = [ZQQSegmentBar segmentBarWithFrame:CGRectMake(0, 0, self.view.width, 35)];
        _segmentBar.delegate = self;
        [self.view addSubview:_segmentBar];
    }
    return _segmentBar;
}

- (UIScrollView *)contentView {
    if (!_contentView) {
        UIScrollView *contentView = [[UIScrollView alloc] init];
        contentView.showsHorizontalScrollIndicator = NO;
        contentView.delegate = self;
        contentView.pagingEnabled = YES;
        [self.view addSubview:contentView];
        _contentView = contentView;
    }
    return _contentView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    // 如果segmentBar在导航栏上的titleView
    if (self.segmentBar.superview != self.view) {
        CGRect frame = self.view.bounds;
        CGFloat y = 64;
        CGFloat h = frame.size.height - 64;
        if (self.tabBarController.tabBar.items > 0) {
            h = h - 49;
        }
        self.contentView.frame = CGRectMake(0, y, frame.size.width, h);
        self.contentView.contentSize = CGSizeMake(self.childViewControllers.count * self.view.width, 0);
    } else {
        // 在导航栏下面
        self.segmentBar.frame = CGRectMake(0, 64, self.view.width, 35);
        CGFloat contentViewY = self.segmentBar.height + self.segmentBar.y;
        // 判断是否有tabbar
        CGFloat tabBarH = 0;
        if (self.tabBarController.tabBar.items > 0) {
            tabBarH = 49;
        }
        CGRect contentFrame = CGRectMake(0, contentViewY, self.view.width, self.view.height - contentViewY - tabBarH);
        self.contentView.frame = contentFrame;
        self.contentView.contentSize = CGSizeMake(self.childViewControllers.count * self.view.width, 0);
    }
    self.segmentBar.selectIndex = self.segmentBar.selectIndex;
}


- (void)setUpWithItems: (NSArray <NSString *>*)items childVCs: (NSArray <UIViewController *>*)childVCs {
    NSAssert(items.count != 0 || items.count == childVCs.count, @"个数不一致, 请自己检查");
    
    self.segmentBar.items = items;
    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
    
    // 添加几个自控制器
    // 在contentView, 展示子控制器的视图内容
    for (UIViewController *vc in childVCs) {
        [self addChildViewController:vc];
    }
    self.contentView.contentSize = CGSizeMake(items.count * self.view.width, 0);
    self.segmentBar.selectIndex = 0;
}

- (void)showChildVCViewsAtIndex: (NSInteger)index {
    if (self.childViewControllers.count == 0 || index < 0 || index > self.childViewControllers.count - 1) {
        return;
    }
    UIViewController *vc = self.childViewControllers[index];
    vc.view.frame = self.view.bounds;
    vc.view.x = index * self.view.width;
    [self.contentView addSubview:vc.view];
    // 滚动到对应的位置
    [self.contentView setContentOffset:CGPointMake(index * self.contentView.width, 0) animated:YES];
}

#pragma mark - 选项卡代理方法 
- (void)segmentBar:(ZQQSegmentBar *)segmentBar didSelectIndex:(NSInteger)toIndex fromIndex:(NSInteger)fromIndex {
    [self showChildVCViewsAtIndex:toIndex];
}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 计算最后的索引
    NSInteger index = self.contentView.contentOffset.x / self.contentView.width;
    
    self.segmentBar.selectIndex = index;
}
@end
